const router = require('./index');
const { Products } = require('../model');
const { Op } = require("sequelize");
const { Sequelize, DataTypes } = require('sequelize');

//数组元素
var productArr = [
    {
        id: 1,
        product: '男装',
        KuCun: 118,
        price: '688',
        PinPaiShan: '海澜之家'
    },
    {
        id: 2,
        product: '女装',
        KuCun: 200,
        price: '580',
        PinPaiShan: '海澜小家'

    },
    {
        id: 3,
        product: '童装',
        KuCun: 2218,
        price: '566',
        PinPaiShan: '海澜之家'

    },
    {
        id: 4,
        product: '西装',
        KuCun: 2200,
        price: '2580',
        PinPaiShan: '阿玛尼'

    }, {
        id: 5,
        product: '泳装',
        KuCun: 1200,
        price: '50',
        PinPaiShan: '鸿星尔克'

    },
]

//查询
let product_Index_fn = async (ctx, next) => {

    let requesr_id = ctx.request.query.keyWord; // 获取请求的id

    //如果请求的id不是空的 那说明有数据
    if (requesr_id) {
        let req_id = parseInt(requesr_id)
        //过滤请求的数据是否存在
        let list = await Products.findAll({
            where: {
                id: {
                    [Op.eq]: req_id
                }
            }
        });
        // let resultObj = productArr.filter(item => {
        // return item.id == requesr_id
        // })
        ctx.body = list;
    } else {
        let list = await Products.findAll()
        ctx.body = list
    }



}


//新增提交
let product_post_fn = (ctx, next) => {

    let product = ctx.request.body.name;
    let KuCun = ctx.request.body.stock;
    let price = ctx.request.body.price;
    let PinPaiShan = ctx.request.body.pps;

    console.log(product);

    //如果数组没有元素 那么就给id初始化1
    if (productArr.length == 0) {

        productArr.id = 1;
        let id = productArr.id
        let obj = {
            id, product, KuCun, price, PinPaiShan
        }

        productArr.push(obj);
        ctx.body = productArr;
    } else {
        //如果数组的商品名称和前端传过来的名称相同 那就不予添加
        chongFuAdd = productArr.filter(item => {
            return item.product == product
        })


        if (chongFuAdd.length > 0) {
            ctx.body = {
                code: 400,
                data: '',
                msg: '不能重复添加商品！'
            }
        } else {
            let id = productArr[productArr.length - 1].id + 1;
            let obj = {
                id, product, KuCun, price, PinPaiShan
            }
            productArr.push(obj);
            ctx.body = productArr;
        }


    }


}



//删除
let product_delete_fn = async (ctx, next) => {
    let res_delete_id = ctx.request.params.id;

    console.log('===============================');
    let delete_id = parseInt(res_delete_id);

    // where id已从数据库中删除
    await Products.destroy({
        where: {
            id: delete_id
        }
    });

    //删除完后 再进行查询数据库中的所有数据 将list返回给前端
    let list = await Products.findAll({});

    ctx.body = list;
}


//更新传过来的id
let product_get_fn = async (ctx, next) => {
    let req_put_id = ctx.request.params.id;

    let put_id = parseInt(req_put_id);


    //更新编辑页面传过来的id 去执行数据库当中的where语句查询   
    let list = await Products.findAll({
        where: {
            id: put_id
        }
    })



    ctx.body = list
}


//这是前端编辑页面的input提交上来的方法
let product_put_fn = async (ctx, next) => {

    // let requestPut_obj = ctx.request.body; //这是input传过来的值
    let req_id = ctx.request.body.id;//请求url的id
    let id = parseInt(req_id);
    let product = ctx.request.body.name;
    let stock = ctx.request.body.stock;
    let price = ctx.request.body.price;
    let pps = ctx.request.body.pps;



    //通过传来的id 到数据库where条件下更新
    await Products.update({ product: product, stock: stock, price: price, pps: pps }, {
        where: {
            id: id
        }
    });
    console.log('====================');
    //再次查询数据库里的全部数据  此时查询 是数据库最新的数据！
    let list = await Products.findAll({});

   



    //把修改后的值响应回去
    ctx.body = list;
}


module.exports = {
    "get /product": product_Index_fn,
    "get /product:id": product_get_fn,
    "put /product": product_put_fn,
    "post /product": product_post_fn,
    "delete /product:id": product_delete_fn,
} 